#!/usr/bin/env python3
# coding:utf-8
import os
import sys
import psutil
from enum import Enum
from copy import deepcopy


class InfoFormat(Enum):
    E_Int = 1
    E_Float = 2
    E_String = 3


def read_file_info(file_path, type: InfoFormat):
    var = None
    with open(file_path, "r") as f:
        buf = f.read()
        if type == InfoFormat.E_Int:
            var = int(buf)
        elif type == InfoFormat.E_Float:
            var = float(var)
        elif type == InfoFormat.String:
            var = deepcopy(buf)
        else:
            print("type not support")

        return var


class Monitor_cpu:
    def __init__(self, cpu_num, monitor_list):
        self.cpu_num = cpu_num
        self.monitor_list = deepcopy(monitor_list)

    def get_info(self):
        for key in self.monitor_list:
            var = self.get_pre_cpu(self.monitor_list[key][0],self.monitor_list[key][1])
            print(key, var)

        print("cpu uage:",psutil.cpu_percent())
        print("sys load：", psutil.cpu_times_percent())
        print("Mem: ", psutil.virtual_memory())

    def get_cpu_percent(self):
        return psutil.cpu_percent()

    def get_system_load(self):
        return os.getloadavg()[0]

    def get_pre_cpu(self, cpu_freq_path, format):
        id = 0
        cpu_freq_list = []
        while id < self.cpu_num:
            path = cpu_freq_path % (id)
            var = read_file_info(path, format)
            if var == None:
                var = 0
            cpu_freq_list.append(var)
            id += 1

        return cpu_freq_list

class Monitor_temp:
    def __init__(self,monitor_list):
        self.monitor_list = deepcopy(monitor_list)

    def get_info(self):
        for key in self.monitor_list:
            var = self.get_pre_var(self.monitor_list[key][0],self.monitor_list[key][1])
            print(key, var)

    def get_pre_var(self, path, format):
        var = read_file_info(path, format)
        if var == None:
            var = 0

        return var

if __name__ == "__main__":
    cpu_num = 8
    RK3588_cpu_list = {
        "cpu_cur_freq": ["/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq",InfoFormat.E_Int]
    }
    monitor_service = Monitor_cpu(cpu_num, RK3588_cpu_list)
    monitor_service.get_info()

    RK3588_temp_list = {
        "soc": ["/sys/class/thermal/thermal_zone0/temp",InfoFormat.E_Int],
        "Big_cpu0": ["/sys/class/thermal/thermal_zone1/temp",InfoFormat.E_Int],
        "Big_cpu2": ["/sys/class/thermal/thermal_zone2/temp",InfoFormat.E_Int],
        "Little_cpu0": ["/sys/class/thermal/thermal_zone3/temp",InfoFormat.E_Int],
        "Little_cpu2": ["/sys/class/thermal/thermal_zone3/temp",InfoFormat.E_Int],
        "gpu": ["/sys/class/thermal/thermal_zone5/temp",InfoFormat.E_Int],
        "npu": ["/sys/class/thermal/thermal_zone6/temp",InfoFormat.E_Int],
    }
    monitor_temp = Monitor_temp(RK3588_temp_list)
    monitor_temp.get_info()
